package me.lc;

import org.json.JSONObject;

import android.util.Log;

import com.mobclick.android.MobclickAgent;
import com.mobclick.android.UmengOnlineConfigureListener;

public class AppControl implements UmengOnlineConfigureListener {

	public static AppControl Instance = new AppControl();
	public static final String On = "on";
	public static final String Off = "off";
	public static final String Channel_Anzuo = "anzhuo";
	public static final String Channel_Anzhi = "anzhi";
	public static final String Channel_All = "all";

	// TODO 每次发布新版时需更新当前版本号
	public static final String CurrentVersion = "89";
	// TODO 每次发布新版时需更新当前版本对应的渠道
	public static final String CurrentChannel = Channel_Anzhi;
	// TODO 每次发布新版时需要更改默认的开关行为true和on对应
	public static final boolean Default_Switcher = true;

	/**
	 * 参数开关，on开启，off关闭，开启表示为启用审核控制
	 */
	public static final String Key_App_Switcher = "app_switcher";
	/**
	 * 试用参数的渠道列表，用逗号分割
	 */
	public static final String Key_App_Channel = "app_channel";
	/**
	 * 试用参数的版本号
	 */
	public static final String Key_App_Version = "app_version";

	private static final String Tag = "AppControl";

	public static void init() {
		// TODO 在OnCreate方法中初始化本地参数
		MobclickAgent.setOnlineConfigureListener(AppControl.Instance);
		MobclickAgent.updateOnlineConfig(FunPeaceActivity.Instance);
		Log.i(Tag,
				String.format(
						"Init success,currentVersion:%s,currentChannel:%s,defaultSwitcher:%s",
						CurrentVersion, CurrentChannel, Default_Switcher));
	}

	/**
	 * 是否为审核状态
	 * 
	 * @return
	 */
	public static boolean isSwitcherOn() {
		try {
			// 首先获取服务器的参数，根据参数决定后续处理，并设置默认值
			String switcher = MobclickAgent.getConfigParams(
					FunPeaceActivity.Instance, Key_App_Switcher);
			String channel = MobclickAgent.getConfigParams(
					FunPeaceActivity.Instance, Key_App_Channel);
			String version = MobclickAgent.getConfigParams(
					FunPeaceActivity.Instance, Key_App_Version);
			Log.i(Tag, String.format(
					"Get online config success. %s:'%s' , %s:'%s' , %s:'%s' .",
					Key_App_Switcher, switcher, Key_App_Channel, channel,
					Key_App_Version, version));
			if (switcher != null && switcher != "" && channel != null
					&& channel != "" && version != null && version != "") {
				if (switcher.trim().toLowerCase().equals(Off)) {
					Log.i(Tag, "Set app_switcher to Off.");
					return false;
				}
				if (switcher.trim().toLowerCase().equals(On)) {
					if (isChannelFit(channel) && isVersionFit(version)) {
						Log.i(Tag, "Set app_switcher to On.");
						return true;
					}
					Log.i(Tag, "Set app_switcher to Off.");
					return false;
				}
			}
		} catch (Exception ex) {
			Log.e(Tag, "get online config error.", ex);
		}
		Log.i(Tag, "Set app_switcher to default:"
				+ (Default_Switcher ? On : Off) + ".");
		return Default_Switcher;
	}

	/**
	 * 根据参数决定是否显示广告
	 * 
	 * @return
	 * 
	 */
	public static boolean isShowAd() {
		// 审核开关开启则不现实广告，否则显示广告
		return !isSwitcherOn();
	}

	/**
	 * 判断渠道号是否为当前对应的
	 * 
	 * @param channel
	 * @return
	 */
	private static boolean isChannelFit(String channel) {
		if (channel.equals(Channel_All))
			return true;
		String[] channels = channel.split(",");
		if (channels != null) {
			for (String c : channels) {
				if (c.trim().toLowerCase().equals(CurrentChannel))
					return true;
			}
		}
		return false;
	}

	/**
	 * 版本号是否匹配
	 * 
	 * @param version
	 * @return
	 */
	private static boolean isVersionFit(String version) {
		if (version.trim().toLowerCase().equals(CurrentVersion)) {
			return true;
		}
		return false;
	}

	@Override
	public void onDataReceived(JSONObject arg0) {
		// TODO 在线参数更新，这里可以重新进行需要的判断处理
		Log.i(Tag, "online config update.");
		// FunPeaceActivity.Instance.initNewPage();
	}

}
